<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;

class WechatCardCode extends Model
{
    protected $hidden = ['mp_id','c_id','user_id','delete_time'];
    protected $deleteTime = 'delete_time';

    use  SoftDelete;

//    protected static function init()
//    {
//        self::afterDelete(function ($cardCode) {
//            if ($cardCode->state == 0) {
//                $wechatCard = new \app\mpbase\library\WechatCard();
//                $wechatCard->disabledCode($cardCode['decrypt_code']);
//            }elseif ($cardCode->state == -2) {
//                $wechatCard = new \app\mpbase\library\WechatCard();
//                $wechatCard->disabledCode($cardCode['decrypt_code']);
//            }
//        });
//    }

    public function info()
    {
        return $this->belongsTo('WechatCard','c_id','id')->bind(
        ['card_id','card_type','logo_url','brand_name','title','sub_title',
            'notice','description','discount','least_cost','reduce_cost','date_type','quantity']);
    }

    /**
     * 保存用户领取的卡券Code
     * @param $mpId
     * @param $userId
     * @param $data
     * @return array
     * @author 清凉Tea <973110519@qq.com>
     */
    public static function saveCode($mpId,$userId,$data)
    {
        $cardCode = self::getByCode($data['code']);
        if($cardCode)
            return ['_code'=>400,'msg'=>'CardCode已存在'];
        $card = WechatCard::getByCardId($data['card_id']);
//        $card->setDec('quantity');
        if($card['date_type'] == 'DATE_TYPE_FIX_TERM'){
            $duration = $card['fixed_begin_term']+$card['fixed_term'];
            $beginTime = strtotime('+'.$card['fixed_begin_term'].'day');
            $endTime = strtotime('+'.$duration.'day');
        }else{
            $beginTime = $card['begin_timestamp'];
            $endTime = $card['end_timestamp'];
        }

        $wechatCard = new \app\mpbase\library\WechatCard();

        $res = $wechatCard->decryptCode($data['code']);
        $data = [
            'mp_id' => $mpId,
            'user_id' => $userId,
            'c_id' => $card['id'],
            'code' => $data['code'],

            'decrypt_code' => $res['code'],
            'begin_time' => $beginTime,
            'end_time' => $endTime,
        ];

        $res = self::insert($data);
        if($res>0)
            return ['_code'=>201,'msg'=>'操作成功'];
        else
            return ['_code'=>500,'msg'=>'操作失败'];
    }

    //核销卡券
    public static function consumeCode($code, $needActivate = false)
    {
        $r = self::destroy(['decrypt_code'=>$code]);
        if($r>0){
            $wechatCard = new \app\mpbase\library\WechatCard();
            $needActivate && $wechatCard->consumeCode($code);
            $wechatCard->consumeCode($code);
            return ['_code'=>200,'msg'=>'操作成功'];
        }else{
            return ['_code'=>500,'msg'=>'操作失败'];
        }
    }

    //核销卡券
    public static function deleteCode($code, $needActivate = false)
    {
        $r = self::destroy(['decrypt_code'=>$code]);
        if($r>0){
            $wechatCard = new \app\mpbase\library\WechatCard();
            $needActivate && $wechatCard->consumeCode($code);
            $wechatCard->consumeCode($code);
            return ['_code'=>200,'msg'=>'操作成功'];
        }else{
            return ['_code'=>500,'msg'=>'操作失败'];
        }
    }

    public static function setState($code,$cardStatus)
    {
        switch($cardStatus){
            //过期
            case 'EXPIRE':
                $state = -1;
                break;
            //赠送 ,禁用 ,消费 ,删除
            case 'GIFTING':
            case 'UNAVAILABLE':
            case 'CONSUMED':
            case 'DELETE':
                $state = -2;
                break;
            default: $state = 0;
        }
        self::where('decrypt_code',$code)->setField('state',$state);
    }

    public function getBeginTimeAttr($value)
    {
        return date('Y-m-d H:i',$value);
    }

    public function getEndTimeAttr($value)
    {
        return date('Y-m-d H:i',$value);
    }
}
